package com.example.sb.util;

import com.example.sb.dao.StockDao;
import com.example.sb.dao.StockOrderDao;
import com.example.sb.dao.UserDao;
import com.example.sb.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @PROJECT_NAME sb @PACKAGE_NAME com.example.sb.util @USER junji @CREATE 2021-12-2021/12/11
 * 8:36 @DESC
 */
@Slf4j
@Service
public class InitParameterService {

  @Autowired private UserDao userDao;
  @Autowired private StockDao stockDao;
  @Autowired private StockOrderDao stockOrderDao;

  @Autowired private StringRedisTemplate redisTemplate;

  /** @author: junjin.zhao
    * @create_on: 2022/2/17 16:03
    * @class_name_on: InitParameterService
    * @version 1.0
    * @description: 启动时初始化参数
    * 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行，并且只会被服务器调用一次，
    * 类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后，init()方法之前运行
    */
  @PostConstruct
  public void initParam() {
    // 查询出所有数据
    List<User> userList = userDao.selectList(null);
    // 通过stream流转换成map,并定义缓存格式
    for (User user : userList) {
      redisTemplate.opsForValue().set(ReidsKey.USER_KEY + user.getId(), user.toString());
    }
    log.info("参数列表初始化成功");
  }
}
